package com.bjgov.scoresys.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.ObjectNotFoundException;

import com.bjgov.scoresys.dao.ProjectItemsDAO;
import com.bjgov.scoresys.entity.ProjectItems;
import com.bjgov.scoresys.util.Page;
import com.bjgov.scoresys.util.PageUtil;
import com.bjgov.scoresys.util.Result;

public class ProjectItemsHandler {
	private static ProjectItemsDAO dao = new ProjectItemsDAO();
	
	public static void save(ProjectItems entity) {
		dao.save(entity);
	}
	
	public static void delete(ProjectItems entity) {
		dao.delete(entity);
	}
	
	public static ProjectItems loadByID(int id) {
		return dao.findById(id);
	}
	
	
	public static List<ProjectItems> loadByScoreItemsID(int ScoreItemsID) {
		return dao.findByItem_ID(ScoreItemsID);
	}
	
	public static List<ProjectItems> getByProjectIdTreeId(int projectId, int treeId) {
		return dao.findByProjectIdTreeId(projectId, treeId);
	}
	
	public static List<ProjectItems> getByProjectIdControlId(int projectId, int controlId) {
		return dao.findByProjectIdControlId(projectId, controlId);
	}
	
	public static List<ProjectItems> getByProject(int projectID) {
		return dao.findByProjectId(projectID);
	}
	
	public static List<ProjectItems> getByProjectItemName(int projectId, String itemName) {
		return dao.findByProjectIdItemName(projectId,itemName);
	}
	
	public static int getControlIdScoreSum(int projectId, int controlId) {
		return dao.getControlIdScoreSum(projectId, controlId);
	}
	
	public static float getNochangeIdScoreSum(String where, int controlId,int projectId) {
		return dao.getNochangeIdScoreSum(where,controlId,projectId);
	}
	
	public static double getProjectIdScoreSum(int projectId) {
		return dao.getProjectIdScoreSum(projectId);
	}

	public static List<ProjectItems> getProjectItemsByIds(String ids) {
		if(StringUtils.isEmpty(ids)) {
			return new ArrayList<ProjectItems>();
		}
		if(ids.endsWith(",")) ids = ids.substring(0, ids.length()-1);
		return dao.findByIds(ids);
	}
	
	//findByTopCateId
	public static List<ProjectItems> loadByTopCateId(int cateid) {
		return dao.findByTopCateId(cateid);
	}
	
	public static Result list(Page page) throws HibernateException,
			ObjectNotFoundException {
		int totalRecords = dao.count();
		page = PageUtil.createPage(page, totalRecords);
		List list = dao.getByPage(page);
		return new Result(list, page);
	}		
	
}
